﻿using System.Collections.Generic;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;

namespace XFSenseUI.XFItems.SenseControls
{
    public class SenseControlRadio : SenseControl
    {
        private int _height;
        private List<SenseControlRadio> _radios = new List<SenseControlRadio>();

        public bool Enabled;

        public SenseControlRadio()
        {
            ItemRectangle = new Rectangle();
            _height = -1;
            ItemType = XFItemType.Clickable;

            _radios.Add(this);
        }

        public override int GetHeight()
        {
            if (_height > -1)
                return _height;

            ItemRectangle = this.GetImageRec("radio-on");
            _height = ItemRectangle.Height;
            return _height;
        }

        public override void ResetHeight()
        {
            _height = -1;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            DrawUI(Enabled ? "radio-on" : "radio-off", g, new Rectangle(x, y, ItemRectangle.Width, ItemRectangle.Height));
        }

        public override XFItemClickResult GetClickResult()
        {
            foreach (var r in _radios)
            {
                r.Enabled = false;
                r.Owner.Invalidate();
            }
            Enabled = true;

            return base.GetClickResult();
        }

        public void AddToGroup(SenseControlRadio radio)
        {
            for (int i = 0; i < _radios.Count; i++)
            {
                var r = _radios[i];
            
                if (!r._radios.Contains(radio))
                    r._radios.Add(radio);
                if (!radio._radios.Contains(r))
                    radio._radios.Add(r);
            }
        }
    }
}
